package com.sky.controller.user;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.PageDTO;
import com.sky.dto.QueryOrderDetailsDTO;
import com.sky.result.Result;
import com.sky.service.OrderDetailService;
import com.sky.service.OrderService;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "C端-用户历史订单接口")
@Slf4j
@RestController("userOrderDetailController")
@RequestMapping("/user/order")
public class OrderDetailController {

    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private OrderService orderService;

    @ApiOperation("根据订单id查询订单详情")
    @GetMapping("/orderDetail/{id}")
    public Result<OrderVO> queryOrderDetail(@PathVariable long id) {
        log.info("查询订单详情,订单id:{}", id);
        OrderVO orderVO = orderDetailService.details(id);
        return Result.success(orderVO);
    }

    @ApiOperation("查询历史订单")
    @GetMapping("/historyOrders")
    public Result<PageDTO<OrderVO>> queryHistoryOrders(QueryOrderDetailsDTO queryOrderDetailsDTO){
        log.info("查询历史订单:{}", queryOrderDetailsDTO);
        PageDTO<OrderVO> pageDTO = orderDetailService.query(queryOrderDetailsDTO);
        return Result.success(pageDTO);
    }

    @ApiOperation("再来一单")
    @PostMapping("/repetition/{id}")
    public Result repetitionOrder(@PathVariable long id){
        log.info("再来一单,订单id:{}", id);
        orderService.repetition(id);
        return Result.success();
    }


    @ApiOperation("取消订单")
    @PutMapping("/cancel/{id}")
    public Result cancel(@PathVariable long id){
        log.info("取消订单,id:{}", id);
        OrdersCancelDTO ordersCancelDTO = new OrdersCancelDTO();
        ordersCancelDTO.setId(id);
        ordersCancelDTO.setCancelReason("用户取消");
        orderService.cancel(ordersCancelDTO);
        return Result.success();
    }
}
